package com.fsy.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fsy.entities.*;
import com.fsy.service.NetfollowsService;
import com.fsy.service.UserrolesService;
import com.fsy.utils.ExcelUtil;
import com.fsy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: CRMProject
 * @Package: com.sxl.controller
 * @Author songxueliang
 * @Date 2022/5/2 15:04
 */
@RequestMapping("/netfollows")
@RestController
@CrossOrigin
public class NetfollowsController {
    @Resource
    private NetfollowsService netfollowsService;
    @Resource
    private UserrolesService userrolesService;

    /**
     * 全查跟踪信息表
     * @param netfollows
     * @return
     */
    @GetMapping("/selectAllNetfollows")
    public Map<String,Object> selectAllNetfollows(Netfollows netfollows){
        List<Netfollows> list = netfollowsService.selectAllByUserId(netfollows);
        int count = netfollowsService.getCount(netfollows);
        return Result.toClient("0","成功",count,list);
    }

    /**
     * 删除跟踪信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Map<String ,Object> deleteById(@PathVariable Integer id){

        boolean rel = netfollowsService.removeById(id);
        Map<String ,Object> rslmap =new HashMap<>();
        if (rel){
            rslmap.put("code","0");
            rslmap.put("msg","删除成功");
        }else{
            rslmap.put("code","1");
            rslmap.put("msg","删除失败");
        }
        return rslmap;
    }

    /**
     * 修改跟踪信息
     * @param netfollows
     * @return
     */
    @PostMapping("/updateNetfollows")
    public Map<String ,Object> updateNetfollows(Netfollows netfollows){
        boolean rel = netfollowsService.updateById(netfollows);
        Map<String ,Object> rslmap =new HashMap<>();
        if (rel){
            rslmap.put("code","0");
            rslmap.put("msg","修改成功");
        }else{
            rslmap.put("code","1");
            rslmap.put("msg","修改失败");
        }
        return rslmap;
    }

    /**
     * 添加跟踪信息
     * @param netfollows
     * @return
     */
    @PostMapping("/createNetfollows")
    public Boolean createNetfollows(Netfollows netfollows){
        return netfollowsService.save(netfollows);
    }
    /**
     * 导出excel
     *跟踪信息
     * @param response
     * @param netfollows
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response, Netfollows netfollows) {
        //查询登录的用户都有什么角色
        List<Userroles> userroles = userrolesService.selectUserrolesById(netfollows.getUserId());
        for (Userroles u : userroles) {
            if (u.getName().equals("咨询经理")) {
                netfollows.setUserId(null);
                break;
            } else {
                netfollows.setUserId(netfollows.getUserId());
            }
        }
        List<Netfollows_export> list = netfollowsService.export(netfollows);
        ExcelUtil.export(response, "students", "students", list, Netfollows_export.class);
    }

}
